#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 窗口大小设置
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);    // 禁止最大化按钮
    setFixedSize(this->width(),this->height());                     // 禁止拖动窗口大小

    // 添加背景图片
    ui->bgd->setGeometry(0, 0, this->width(), this->height());      // 调整窗口大小
    add_bgd_img(ui->bgd, QString(":/bgi/images/bgd/bgimg.png"));

    // 添加两个按钮的图标
    QIcon pet_icon;
    pet_icon.addFile(":/icon/images/icon/petball.png");
    ui->petbtn->setIcon(pet_icon);
    ui->petbtn->setIconSize(QSize(ui->petbtn->width(), ui->petbtn->height()));
    pet_icon.addFile(":/icon/images/icon/trainer.png");
    ui->trainer->setIcon(pet_icon);
    ui->trainer->setIconSize(QSize(ui->trainer->width(), ui->trainer->height()));

    // 加载任务栏
    add_bgd_img(ui->bar, QString(":/bar/images/bar.png"));

    // 加载人物：
    init_perpath();
    init_order();

    // pokemon窗口
    pokemonIsOpen = false;     // 默认关闭；

    // trainer窗口
    trainerIsOpen = false;

    // 商店窗口
    shopIsOpen = false;

    fightIsOpen = false;

    // 窗口开启权限：
    setOpenRight();   // 权限初始化设置

    // 登录界面调用：
    loginpage = new login();
    loginpage->show();
    connect(loginpage, SIGNAL(success(int)), this, SLOT(openMainWindow(int)));

    walk(DOWN);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init_perpath()
{
    for (int i = 0; i<12; i++) {
        this->perpath[i] = QString(":/person/images/per1 (%1).png").arg(i+1);
    }
}

void MainWindow::init_order()
{
    this->order = 0;
    next = true;    // order增模式
}

void MainWindow::walk(MainWindow::Direction d)
{
    if (order == 3) {
        order = 0;
    }
    if(next) {
        add_bgd_img(ui->Person, QString(perpath[order + (d * 3)]));
        order++;
        if (order == 3) {
            next = !next;
            order = 1;
        }
    } else {
        add_bgd_img(ui->Person, QString(perpath[order + (d * 3)]));
        order--;
        if(order == -1) {
            next = !next;
            order = 1;
        }
    }
}

template<typename T>
void MainWindow::add_bgd_img(T *tool, QString path)
{
    QImage* img = new QImage(path);      // 这里不能分开。。。
    img->scaled(tool->size(), Qt::IgnoreAspectRatio);
    tool->setScaledContents(true);
    tool->setPixmap(QPixmap::fromImage(*img));
}


void MainWindow::on_petbtn_clicked()      // 用来响应按钮点击，以及界面关闭后的参数情况
{
    if(openRight == true) {
        pokemon = new Widget(this, id);
//        pokemon->setParent(this);       // 窗口依附
        connect(pokemon, SIGNAL(Exitpoke()), this, SLOT(on_petbtn_clicked()));
        pokemon->move(270, 150);
        pokemon->show();
        pokemon->on_pet_1_clicked();
        pokemonIsOpen = true;
        setOpenRight();
    }
    else {
        if(pokemonIsOpen == true) {
            pokemon->close();
            pokemonIsOpen = false;
            setOpenRight();
        }
    }
}

void MainWindow::on_trainer_clicked()
{
    if(openRight == true) {
        P_trainer = new trainer(this, id);
//        P_trainer->setParent(this);
        connect(P_trainer, SIGNAL(ExitTrainer()), this, SLOT(on_trainer_clicked()));
        P_trainer->show();
        P_trainer->move(550, 100);
        trainerIsOpen = true;
        setOpenRight();
    }
    else {
        if(trainerIsOpen == true) {
            P_trainer->close();
            trainerIsOpen = false;
            setOpenRight();
        }       // 其他页面打开的情况则按钮不响应
    }
}

void MainWindow::setOpenRight()
{
    openRight = !(pokemonIsOpen || trainerIsOpen || shopIsOpen || hospitalIsOpen || fightIsOpen);    // 有一个窗口开启则开启权限关闭
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_W:
        walk(UP);
        ui->Person->move(ui->Person->x(), ui->Person->y()-10);
        break;
    case Qt::Key_S:
        walk(DOWN);
        ui->Person->move(ui->Person->x(), ui->Person->y()+10);
        break;
    case Qt::Key_A:
        walk(LEFT);
        ui->Person->move(ui->Person->x()-10, ui->Person->y());
        break;
    case Qt::Key_D:
        walk(RIGHT);
        ui->Person->move(ui->Person->x()+10, ui->Person->y());
        break;
    default:
        break;
    }
}

void MainWindow::on_Shop_clicked()
{
    if(openRight == true) {
        shop = new Shop(this, id);
//        shop->setParent(this);
        connect(shop, SIGNAL(ExitShop()), this, SLOT(on_Shop_clicked()));
        shop->move(400, 250);
        shop->show();
        shopIsOpen = true;
        setOpenRight();
    }
    else {
        if(shopIsOpen == true) {
            shop->close();
            shopIsOpen = false;
            setOpenRight();
        }
    }
}

void MainWindow::on_Hospital_clicked()
{
    if(openRight == true) {
        hospital = new Hospital(this, id);
        connect(hospital, SIGNAL(ExitHospital()), this, SLOT(on_Hospital_clicked()));
        hospital->move(0, 0);
        hospital->show();
        hospitalIsOpen = true;
        setOpenRight();
    }
    else {
        if(hospitalIsOpen == true) {
            hospital->close();
            hospitalIsOpen = false;
            setOpenRight();
        }
    }

}

void MainWindow::openMainWindow(int id)
{
    this->show();
    this->id = id;
}

void MainWindow::on_enter_fight_clicked()
{
    loadingpage = new Loading();
    loadingpage->setParent(this);
    loadingpage->move(500, 300);
    connect(loadingpage, SIGNAL(clo()), this, SLOT(show_fight_page()));
    loadingpage->Show_and_close();
}

void MainWindow::show_fight_page()
{
    if (openRight == true) {
        fightpage = new Fight(this, id);
        fightpage->move(0, 0);
        fightpage->show();
        connect(fightpage, SIGNAL(clo()), this, SLOT(after_close_fightpage()));
        fightIsOpen = true;
        setOpenRight();
    }

}

void MainWindow::after_close_fightpage()
{
    fightpage->close();
    fightIsOpen = false;
    setOpenRight();
}
